<?php

namespace app\api\controller\settings;

use app\api\model\settings\JoinApplyModel;
use app\common\library\easywechat\AppMp;
use app\Request;

/**
 * 商品分类控制器
 */
class JoinApply
{
    public function add(Request $request)
    {
        $params = $request->param();
        $count = JoinApplyModel::where('phone', '=', $params['phone'])->count('id');
        if ($count > 0) return $this->renderError('请勿重复提交');
        JoinApplyModel::createData($params);
        return $this->renderSuccess('操作成功');
    }

    public function getWxConfig(Request $request)
    {
        $url = $request->param('url');
        $app = AppMp::getApp('10001');
        $app->jssdk->setUrl($url);
        $signPackage = $app->jssdk->buildConfig([], false);
        return $this->renderSuccess('', json_decode($signPackage));
    }

    /**
     * 返回操作成功json
     */
    protected function renderSuccess($msg = 'success', $data = [])
    {
        return json($this->renderJson(1, $msg, $data));
    }

    /**
     * 返回操作失败json
     */
    protected function renderError($msg = 'error', $data = [], $code = 0)
    {
        return json($this->renderJson($code, $msg, $data));
    }

    /**
     * 返回封装后的 API 数据到客户端
     */
    protected function renderJson($code = 1, $msg = '', $data = [])
    {
        return compact('code', 'msg', 'data');
    }

}
